#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        int target = 0, n = nums.size();
        for (int a : nums) target += a;
        target -= x;
        if (target < 0)
            return -1;
        int sum = 0, len = -1;
        for (int left = 0, right = 0; right < n; right++)
        {
            sum += nums[right];
            while (sum > target)
                sum -= nums[left++];
            if (sum == target)
                len = max(len, right - left + 1);
        }
        return len == -1 ? -1 : n - len;
    }
};
class Solution {
public:
    int longestOnes(vector<int>& nums, int k) {
        int left = 0, right = 0, len = 0, n = nums.size(), zero = 0;
        while (right < n)
        {
            if (nums[right] == 0) zero++;
            while (zero > k)
                if (nums[left++] == 0) zero--;
            len = max(len, right - left + 1);
            right++;
        }
        return len;
    }
};